<template>
    <el-container class="home-container">
        <!-- 头部 -->
        <el-header>
            <div>
                <img src="@/assets/logo.png" alt="" />
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单 -->
                <el-menu text-color="#fff" background-color="#333744" active-text-color="#409eff" unique-opened
                    :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                    <!-- 一级菜单 -->
                    <el-submenu v-for="item in menulist" :key="item.id" :index="item.id.toString()">
                        <!-- 一级菜单模板区 -->
                        <template slot="title">
                            <i :class="iconMap[item.path]"></i>
                            <span>{{ item.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item v-for="child in item.children" :index="`/${child.path}`" :key="child.id"
                            @click="saveNavState('/' + child.path)">
                            <!-- 二级菜单模板区 -->
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ child.authName }}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 内容主体 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
    name: "Home",
    data() {
        return {
            menulist: [],
            isCollapse: false,
            iconMap: {
                users: "el-icon-user-solid",
                rights: "el-icon-lock",
                goods: "el-icon-goods",
                orders: "el-icon-date",
                reports: "el-icon-s-data",
            },
            activePath: "",
        };
    },
    created() {
        this.getMenuList()
        this.activePath = sessionStorage.getItem("activePath")
    },
    methods: {
        logout() {
            sessionStorage.clear();
            this.$router.push("/login");
        },
        getMenuList() {
            this.$http({
                url: 'menus',
                method: 'get'
            }).then((res) => {
                // console.log(res);
                this.menulist = res.data;
            });
        },
        // 点击按钮，切换按钮的展开和折叠
        toggleCollapse() {
            this.isCollapse = !this.isCollapse;
        },
        // 保存导航栏状态
        saveNavState(activePath) {
            sessionStorage.setItem("activePath", activePath);
            this.activePath = activePath;
        },
    },
};
</script>

<style scoped lang="less">
.home-container {
    height: 100%;
}

.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    > div {
        display: flex;
        align-items: center;

        span {
            margin-left: 15px;
        }

        img {
            width: 50px;
        }
    }
}

.el-menu {
    border-right: none;
}

.el-aside {
    background-color: #333744;
}

.el-main {
    background-color: #eaedf1;
}

.toggle-button {
    background-color: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
